package org.coderfun.boot.core.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.session.Session;
import org.coderfun.boot.core.BootConst.KickoutMode;
import org.coderfun.boot.core.dao.RolePermissionDAO;
import org.coderfun.boot.core.entity.RolePermission;
import org.coderfun.boot.core.entity.RolePermission_;
import org.coderfun.boot.core.entity.UserRole;
import org.coderfun.boot.core.entity.UserRole_;
import org.coderfun.boot.core.shiro.SessionUser;
import org.coderfun.boot.core.shiro.ShiroSessionUtil;
import org.coderfun.boot.core.shiro.UserAuthRealm;
import org.crazycake.shiro.RedisSessionDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import klg.common.dataaccess.BaseServiceImpl;
import klg.query.jpa.expr.AExpr;

/**
 *
 * 
 * Generated by fieldmeta at 2018-10-23T14:08:29+08:00
 *
 */

@Service
public class RolePermissionServiceImpl extends BaseServiceImpl<RolePermission, Long> implements RolePermissionService {
	@Autowired
	RolePermissionDAO rolePermissionDAO;
	
	@Autowired
	UserService userService;
	
	@Autowired
	UserRoleService userRoleService;
	
	@Transactional
	@Override
	public void updateRolePermissions(Long roleId, List<Long> newPermissionIds) {
		List<Long> oldPermissionIds = findPermissionIdsByRoleId(roleId);
		// 需要删除
		List<RolePermission> needToBeDeleted = new ArrayList<>();
		for (Long oldPermissionId : oldPermissionIds) {
			if (!newPermissionIds.contains(oldPermissionId)) {
				RolePermission rolePermission = rolePermissionDAO.getOne(AExpr.eq(RolePermission_.roleId, roleId), 
						AExpr.eq(RolePermission_.permissionId, oldPermissionId));
				needToBeDeleted.add(rolePermission);
			}
		}
		// 批量删除
		rolePermissionDAO.delete(needToBeDeleted);

		// 需要添加
		List<RolePermission> needToBeAdded = new ArrayList<>();
		for (Long newPermissionId : newPermissionIds) {
			if (!oldPermissionIds.contains(newPermissionId)) {
				RolePermission rolePermission = new RolePermission();
				rolePermission.setPermissionId(newPermissionId);
				rolePermission.setRoleId(roleId);
				needToBeAdded.add(rolePermission);
			}
		}
		// 批量保存
		rolePermissionDAO.save(needToBeAdded);
		
		//清空权限缓存
		List<UserRole> userRoles = userRoleService.findList(AExpr.eq(UserRole_.roleId, roleId));
		for(UserRole userRole:userRoles){
			//踢出用户，达到清空权限缓存的目的，可配置集群
			userService.kickout(userRole.getUserId(), KickoutMode.ALL);
		}
				
	}

	@Override
	public List<Long> findPermissionIdsByRoleId(Long roleId) {
		// TODO Auto-generated method stub
		return rolePermissionDAO.findPermissionIdsByRoleId(roleId);
	}
	
	@Transactional
	@Override
	public void deleteInBatchByRoleId(Long roleId) {
		// TODO Auto-generated method stub
		List<RolePermission> rolePermissions = rolePermissionDAO.findList(AExpr.eq(RolePermission_.roleId, roleId));
		rolePermissionDAO.deleteInBatch(rolePermissions);
	}

	@Transactional
	@Override
	public void deleteInBatchByPermissionId(Long permissionId) {
		// TODO Auto-generated method stub
		List<RolePermission> rolePermissions = rolePermissionDAO.findList(AExpr.eq(RolePermission_.permissionId, permissionId));
		rolePermissionDAO.deleteInBatch(rolePermissions);
	}
}
